/**
 * Created by Administrator on 2018/8/17.
 */
InitCertusJqNumLimit();
// 资产库可用资产信息
var purchaseProductDatas = new Array();
$$scope.submitter = {};
$$scope.select = {};
$$scope.select.itAcceptanceSelect = [
    {name: "合格", code: "1"}, {name: "不合格", code: "0"}
]
$$scope.select.acceptanceResolveSelect = [
    {name: "合格", code: "1"}, {name: "不合格", code: "0"}
]

$$scope.select.yesorNo = [
    {name: "是", code: "1"}, {name: "否", code: "0"}
]
var purchaseStepEntity = {
    "processInstanceId": $CertusJsonAjax.getProInstanceId(),
    "stepInstanceId": $CertusJsonAjax.getStepInstanceId(),
}
$$scope.submmitData = {};
$$scope.submmitData.purchaseStepEntity = {
    "processInstanceId": $CertusJsonAjax.getProInstanceId(),
    "stepInstanceId": $CertusJsonAjax.getStepInstanceId(),
}

var bqArray = {};
var oldBqArray = {};

// 采购记录
var proc1Columns = [
    {
        id: 'commodityCateName',
        title: '类目',
        width: 73,
        type: 'string',
        columnClass: 'text-center',
    }, {
        id: 'commodityClassName',
        title: '类别',
        width: 73,
        type: 'string',
        columnClass: 'text-center',
    }, {
        id: 'name',
        title: '名称',
        width: 73,
        type: 'string',
        columnClass: 'text-center',
    }, {
        id: 'descAndSpec',
        title: '品名及规格',
        width: 200,
        type: 'string',
        columnClass: 'text-left',
    }, {
        id: 'quantity',
        title: '数量',
        width: 100,
        type: 'string',
        columnClass: 'text-center',
    }, {
        id: 'estimatedAmount',
        title: '预计金额',
        width: 150,
        type: 'string',
        columnClass: 'text-center',
        resolution: function (value, record, column, grid, dataNo, columnNo) {
            var content = '￥' + value + '元';
            return content;
          }
    }, {
        id: 'remark',
        title: '备注',
        type: 'string',
        columnClass: 'text-left',
    }, {
        id: 'operation',
        title: '操作',
        width: 120,
        type: 'string',
        columnClass: 'text-center',
        resolution: function (value, record, column, grid, dataNo, columnNo) {
            var content = '';
            content += '<button class="btn btn-xs btn-success" onclick="showNowQuote(\'' + record.biCode + '\')"><i class="icon-eye-open"></i>  查看</button>';
            content += '  ';
            return content;
        }
    }];


function showNowQuote(biCode) {
    if (biCode) {
        belowGrid.exhibitDatas = bqArray[biCode];
        belowGrid.refresh(false);
        $(".hdlog").show();
        $(".hdgbuy").show();
    }
}

function showAllQuote() {
    var tempArray = new Array();
    if (bqArray) {
        for (var j in bqArray) {
            tempArray = tempArray.concat(bqArray[j]);
        }
    }
    belowGrid.exhibitDatas = tempArray;
    belowGrid.refresh(false);

    $(".hdgbuy").show();
}

// 采购比价单信息
var pryslColumns = [
//     {
//     id: 'no',
//     title: '序号',
//     width: 50,
//     type: 'string',
//     columnClass: 'text-center',
// }, 
{
    id: 'supplierName',
    title: '供应商名称',
    width: 150,
    type: 'string',
    columnClass: 'text-center',
}, {
    id: 'name',
    title: '名称',
    width: 150,
    type: 'string',
    columnClass: 'text-center',
}, {
    id: 'specification',
    title: '规格(参数)',
    // width: 200,
    type: 'string',
    columnClass: 'text-left',
}, {
    id: 'unit',
    title: '单位',
    width: 54,
    type: 'string',
    columnClass: 'text-center',
}, {
    id: 'recievedNum',
    title: '实收数量',
    width: 74,
    type: 'string',
    columnClass: 'text-center',
}, {
    id: 'acceptanceResolve',
    title: '采购单位验收',
    width: 100,
    type: 'string',
    columnClass: 'text-center',
    resolution: function (value, record, column, grid, dataNo, columnNo) {
        if (value) {
            if (value == 1) {
                return "合格"
            } else {
                return "不合格"
            }
        }
        return "";
    }
}, {
    id: 'itAcceptance',
    title: '验收单位验收',
    width: 100,
    type: 'string',
    columnClass: 'text-center',
    resolution: function (value, record, column, grid, dataNo, columnNo) {
        if (value) {
            if (value == 1) {
                return "合格"
            } else {
                return "不合格"
            }
        }
        return "";
    }
}, {
    id: 'p8',
    title: '备注',
    type: 'string',
    columnClass: 'text-left',
    hideType: 'lg|md|sm|xs'
}];


function openAcceptance(bqCode, biCode) {

    if (bqCode && biCode) {
        $$scope.sbsb = {}
        $$scope.sbsb.bqCode = bqCode
        $$scope.sbsb.biCode = biCode;
        var bqArray2 = bqArray[biCode];
        if (bqArray2) {
            for (var i in bqArray2) {
                $$scope.acceptance = {}
                if (bqArray2[i].bqCode == bqCode) {
                    if(bqArray2[i].recievedNum){
                        $$scope.acceptance.recievedNum = bqArray2[i].recievedNum;
                    }
                    if(bqArray2[i].unit){
                        $$scope.acceptance.unit = bqArray2[i].unit
                    }
                    if(bqArray2[i].acceptanceResolve){
                        $$scope.acceptance.acceptanceResolve = bqArray2[i].acceptanceResolve
                    }
                    if( bqArray2[i].itAcceptance){
                        $$scope.acceptance.itAcceptance = bqArray2[i].itAcceptance
                    }

                    $$scope.$apply();
                }
            }

        }
    }
    $("#myModal").modal('show');
}


$$scope.admitAcceptance = function (bqCode, biCode) {

    if(certusValidateNotnullPolyfill()){
        if (bqCode && biCode) {
            $$scope.sbsb.bqCode = bqCode
            $$scope.sbsb.biCode = biCode;
            var bqArray2 = bqArray[biCode];
            if (bqArray2) {
                for (var i in bqArray2) {
                    if (bqArray2[i].bqCode == bqCode) {
                        bqArray2[i].recievedNum = $$scope.acceptance.recievedNum
                        bqArray2[i].unit = $$scope.acceptance.unit
                        bqArray2[i].acceptanceResolve = $$scope.acceptance.acceptanceResolve
                        bqArray2[i].itAcceptance = $$scope.acceptance.itAcceptance
                    }
                }

            }
            belowGrid.exhibitDatas = bqArray[biCode];
            belowGrid.refresh(false);
        }
        $("#myModal").modal('hide');
    }
}


function shupload(bqCode, biCode) {
    $(".hdupload").show();
    $$scope.agreementAaaa = {};
    $$scope.agreementAaaa.bqCode = bqCode;
    $$scope.agreementAaaa.biCode = biCode;
}

function openAgreementDownloadList(bqCode, biCode) {
    if (biCode && bqCode) {
        var nowBq = bqArray[biCode];
        if (nowBq) {
            for (var i in nowBq) {
                if (nowBq[i].supplierFile && nowBq[i].bqCode == bqCode) {
                    $$scope.modalContent = eval(nowBq[i].agreementFile)
                }
            }
        }
    }
    // $$scope.modalContent = data.data;
    $$scope.ok = function () {
        $modalInstance.close(); // 点击确认后关闭弹窗
    };
    $$scope.cancel = function () {
        $modalInstance.dismiss('cancel'); // 点击取消后撤销模态框
    }
    var modalContent = $$scope.modalContent;

    $fileListService.openConfirmWindow("提示", modalContent).then(function (res) {
    });
}


function hdgs() {
    $(".hdgs").hide();
}

//打开 打开 历史库下载列表
function downOldAgreement(bqCode, biCode) {
    if (biCode && bqCode) {
        var nowBq = oldBqArray[biCode];
        if (nowBq) {
            for (var i in nowBq) {
                if (nowBq[i].supplierFile && nowBq[i].bqCode == bqCode) {
                    $$scope.modalContent = eval(nowBq[i].agreementFile)
                }
            }
        }
    }
    // $$scope.modalContent = data.data;
    $$scope.ok = function () {
        $modalInstance.close(); // 点击确认后关闭弹窗
    };
    $$scope.cancel = function () {
        $modalInstance.dismiss('cancel'); // 点击取消后撤销模态框
    }
    var modalContent = $$scope.modalContent;

    $fileListService.openConfirmWindow("提示", modalContent).then(function (res) {
    });
}


var belowGrid;
var aboveGrid;
var brCode;
$CertusJsonAjax.ajax("purchaseBaseInfoAndQuoteHaveAgreement", {
    "processInstanceId": $CertusJsonAjax.getProInstanceId(),
    "stepInstanceId": $CertusJsonAjax.getStepInstanceId()
}, function (res) {
    if (res.data) {

        $$scope.headerMap = res.data.headerMap;
        brCode = res.data.headerMap.brCode;
        // purchaseStepEntity.buyingRequisitionCode = res.data.headerMap.brCode;
        $$scope.submmitData.purchaseStepEntity.buyingRequisitionCode = res.data.headerMap.brCode;

            $$scope.submmitData.purchaseStepEntity.projId = res.data.headerMap.projId;
        var datas = res.data.buyings;
        var priceOption1 = {
            lang: 'zh-cn',
            ajaxLoad: false,
            extraWidth: 20,
            // loadURL:'url',
            exportFileName: '采购单条目信息',
            datas: datas,
            columns: proc1Columns,
            gridContainer: 'priceContainer',
        };
        aboveGrid = $.fn.dlshouwen.grid.init(priceOption1);
        aboveGrid.load();

        //初始化 报价 文件 数据
        if (datas) {
            for (var i in datas) {
                if (datas[i].biCode) {
                    bqArray[datas[i].biCode] = datas[i].buyingQuoteItemEntities;
                }
            }
            //初始化 历史报价 文件 数据
            for (var i in datas) {
                if (datas[i].biCode) {
                    oldBqArray[datas[i].biCode] = datas[i].oldBuyingQuoteItemEntities;
                }
            }
        }

        var prcListOption = {
            lang: 'zh-cn',
            ajaxLoad: false,
            // loadURL:'url',
            check : true,
            checkWidth : 20,
            extraWidth: 20,
            exportFileName: '供应商列表',
            datas: [],
            columns: pryslColumns,
            gridContainer: 'pryslContainer',
            tools: '',
            pageSize: 10,
            pageSizeLimit: [10, 15, 20]
        };
        belowGrid = $.fn.dlshouwen.grid.init(prcListOption);
        belowGrid.load();
    }
})
$$scope.approvalAgreementSubmit = function () {

    if (certusValidateNotnull()) {
        $$scope.submmitData.purchaseStepEntity.findWhoType = 'A';
        $$scope.submmitData.buyingQuoteItemEntities = bqArray;
        $CertusJsonAjax.ajax("confirmPrintPurchase", $$scope.submmitData, function (res) {
            if (res.data) {
                if (res.data == "success") {
                    findBacklogPerson($CertusFormAjax,$CertusJsonAjax.getStepInstanceId());
                    window.history.back();
                    return;
                }
                layer.msg(res.data);
            }
        })
    }
}

//文件列表下载
function downLoad(id) {
    var fileID = $(id).parent().parent().find(".fileDownloadPath").val();
    $CertusFormAjax.download("downloadFile", "fileId=" + fileID);
}

function pryslGridCheckedRecordsLength(){

    var recordCount = belowGrid.getCheckedRecords().length;
    if(recordCount <= 0){
        layer.msg("请至少勾选一条数据！");
    }else{
        //console.log(recordCount);
        $$scope.first = {};
        $$scope.$apply();
        if(recordCount > 1){
            layer.msg("系统默认选择 第一条");
        }
        var vr = new Array();
        vr.push(belowGrid.getCheckedRecords()[0]);
        $$scope.second = vr;
        $('#mySodal').modal();
    }
}

function openPrintView(){
    if(certusValidateNotnullPolyfill()){
        $("#main-content").hide();
        $("#main-content-1").show();
        $('#mySodal').modal('hide');
    }
}

function closePrintView(){
    $("#main-content").show();
    $("#main-content-1").hide();
}
